package com.tangding.aspect.god;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.base.Strings;
import com.tangding.common.exception.GException;
import com.tangding.common.logx.LogHelp;
import com.tangding.common.tools.ErrorConstants;
import com.tangding.common.utils.AES128;
import com.tangding.config.Config;
import com.tangding.hprose.api.HproseBase;
import com.tangding.net.pool.BaseNettySessionPool;
import com.tangding.service.dao.user.LoginRepository;
import com.tangding.service.dao.user.UserRepository;
import com.tangding.service.entity.user.User;

/**
 * @description 
 *
 * @author Ranger
 * @version 2017年12月23日_上午10:53:36
 *
 */
@Component
public class GodHproseCommand extends GodBase implements HproseBase {
	
	protected static BaseNettySessionPool pool;
	
	@Autowired
	public void setPool(BaseNettySessionPool pool) {
		GodHproseCommand.pool = pool;
	}
	
	protected static UserRepository userRepository;
	
	@Autowired
	public void setUserRepository(UserRepository userRepository) {
		GodHproseCommand.userRepository = userRepository;
	}
	
	private static LoginRepository loginRepository;

	@Autowired
	public void setLoginRepository(LoginRepository loginRepository) {
		GodHproseCommand.loginRepository = loginRepository;
	}
	
	public static Object Onlines(Object params){
		Map<String, Object> map = new HashMap<>();
		if (!CheckMe(params)) {
			map.put("god", false);
		}else {
			Collection<String> onlines = pool.onlines();
			map.put("onlineNum", onlines.size() - 1);
			List<Map<String, Object>> list = new ArrayList<>();
			if (onlines.size() > 0) {
				for (String id : onlines) {
					if (godAccount.equals(id)) {
						continue;
					}
					User user = userRepository.getUserByUid(id);
					if (user == null) {
						continue;
					}
					Map<String, Object> m = new HashMap<>();
					m.put("id", id);
					m.put("nickName", user.getName());
					list.add(m);
				}
			}
			map.put("onlines", list);
		}
		return map;
	}
	
	/**
	 * 检测能否弹出踢出管理员的窗口
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static boolean CheckMe(Object params) {
		try {
			Map<String, Object> user = (Map<String, Object>) params;
			String uid = (String) user.get("uid");
			String seceret = (String) user.get("seceret");
			String resultString = AES128.Decrypt(seceret, Config.AES128_KEY);
			if (Strings.isNullOrEmpty(resultString) || resultString.indexOf("|") == -1 || resultString.split("[|]").length < 2) {
				return false;
			}
			String[] cells = resultString.split("[|]");
			String checkUid = cells[0];
			String checkToken = cells[1];
			if (!checkUid.equals(uid)) {
				return false;
			}
			
			String loginToken = loginRepository.get(uid);
			if (Strings.isNullOrEmpty(checkToken) || !checkToken.equals(loginToken)) {//登录已经非法
				throw new GException(ErrorConstants.LOGIN_EXPIRE);
			}
			
			if (godAccount.equals(uid)) {
				return true;
			}
			return false;
		} catch (GException e) {
			LogHelp.doLogAppErr(e);
			return false;
		}
	}

	@SuppressWarnings("unchecked")
	public static boolean TickoutAllAdmin(Object params) {
		Map<String, Object> user = (Map<String, Object>) params;
		String uid = (String) user.get("uid");
		
		if (!CheckMe(params)) {
			return false;
		}
		
		if (godAccount.equals(uid)) {
			Collection<String> onlines = pool.onlines();
			if (onlines != null && onlines.size() > 0) {
				onlines.forEach(id -> {
					if (godAccount.equals(id)) {//不踢出上帝帐号
						
					}else {
						pool.doResponse(id, "ForceQuit", null);
					}
				});
			}
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public static boolean TickoutAdmin(Object params, Object me) {
		try {
			if (!CheckMe(me)) {
				return false;
			}
			
			Map<String, Object> pa = (Map<String, Object>) params;
			String adminId = (String) pa.get("adminId");
			if (godAccount.equals(adminId)) {
				return false;
			}
			
			Collection<String> onlines = pool.onlines();
			if (onlines != null && onlines.size() > 0) {
				onlines.forEach(id -> {
					if (adminId.equals(id)) {
						pool.doResponse(id, "ForceQuit", null);
					}
				});
			}
			return true;
		} catch (Exception e) {
			LogHelp.doLogException(e);
			return false;
		}
	}
}
